package com.zwan.oam_rtc.controller;

import com.zwan.oam_rtc.bean.entity.CreateRoomData;
import com.zwan.oam_rtc.bean.entity.InviteSipData;
import com.zwan.oam_rtc.bean.req.*;
import com.zwan.oam_rtc.service.RtcmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/webrtc")
public class OamController {
	@Autowired
	RtcmsService rtcmsService;

	//登录，默认都通过
	@RequestMapping(value="/rtc/login",method=RequestMethod.POST)
	public Map<String, Object> login(@RequestBody LoginReq req) {

		Map<String, Object> result = new HashMap<>();
		Map<String, Object> data = new HashMap<>();
		
		data.put("realName", req.getUserName());
		data.put("sender", req.getUserName());
		data.put("companyId", 0);
		
		if(req.getUserName().equalsIgnoreCase("zwan") && 
		(req.getPassword().equals("Zen@dvlp") || req.getPassword().equals("833845b7396a7fa0e683843491fc20d6"))) {
			result.put("status", 200);	
			result.put("data", data);		
		}else {
			result.put("status", 403);
		}
		return result;
	}

	//房间列表，向rtcms发送查询
	@RequestMapping(value="/rtc/roomList",method=RequestMethod.POST)
	public String roomList() {
		return rtcmsService.getConfListData();
	}

	//房间简易列表，向rtcms发送查询
	@RequestMapping(value="/rtc/roomBriefList",method=RequestMethod.POST)
	public String roomBriefList() {
		return rtcmsService.getConfBriefListData();
	}

	//某会议室信息，向rtcms发送查询
	@RequestMapping(value="/rtc/queryConfInfo",method=RequestMethod.GET)
	public String queryConfInfo(@RequestParam String room) {
		return rtcmsService.getConfData(room);
	}

	//某会议室参会人员列表，向rtcms发送查询
	@RequestMapping(value="/rtc/queryConfMemberInfo2",method=RequestMethod.POST)
	public String queryConfMemberInfo2(@RequestBody QueryConfMemberInfoReq req) {
		return rtcmsService.getUserData(req);
	}

	//向某会议室发送消息
	@RequestMapping(value="/rtc/confsms",method=RequestMethod.POST)
	public String confSMS(@RequestBody ConfSMSReq req) {
		return rtcmsService.confSMS(req);
	}

	//查询已经注册到服务器的SIP设备信息
	@RequestMapping(value="/rtc/queryDevice",method=RequestMethod.POST)
	public String queryDevice() {
		return rtcmsService.queryDevice();
	}

	//呼叫sip设备
//	{"room": "test"}
//	{"error_code":200,"error":"","data":{"room":"test"}}
	@RequestMapping(value="/rtc/createRoom",method=RequestMethod.POST)
	public String inviteSip(@RequestBody CreateRoomData req) {
		return rtcmsService.createRoom(req);
	}

	@RequestMapping(value="/rtc/deleteRoom",method=RequestMethod.POST)
	public String deleteRoom(@RequestBody CreateRoomData req) {
		return rtcmsService.deleteRoom(req);
	}

	//呼叫sip设备
	@RequestMapping(value="/rtc/inviteSip",method=RequestMethod.POST)
	public String inviteSip(@RequestBody InviteSipData req) {
		return rtcmsService.inviteSip(req);
	}

	//查询P模块信息
	@RequestMapping(value="/rtc/zwmpList",method=RequestMethod.POST)
	public String zwmpList() {
		return rtcmsService.zwmpList();
	}

	//查询P模块信息
	@RequestMapping(value="/rtc/zwmpDetail",method=RequestMethod.POST)
	public String zwmpDetail(@RequestBody zwmpDetailReq req) {
		return rtcmsService.zwmpDetail(req);
	}

	//在某房间中开始录像
	@RequestMapping(value="/rtc/startRecord",method=RequestMethod.POST)
	public String startRecord(@RequestBody startRecordReq req) {
		return rtcmsService.startRecord(req);
	}

	//在某房间中停止录像
	@RequestMapping(value="/rtc/stopRecord",method=RequestMethod.POST)
	public String stopRecord(@RequestBody stopRecordReq req) {	return rtcmsService.stopRecord(req); }

	//在某房间中无丢帧切换录像
	@RequestMapping(value="/rtc/switchRecord",method=RequestMethod.POST)
	public String switchRecord(@RequestBody switchRecordReq req) {	return rtcmsService.switchRecord(req); }

	//截图
	@RequestMapping(value="/rtc/snapRecord",method=RequestMethod.POST)
	public String snapRecord(@RequestBody snapRecordReq req) {	return rtcmsService.snapRecord(req); }

	//注册成为房间事件接收者
	@RequestMapping(value="/rtc/registerRoomObserver",method=RequestMethod.POST)
	public String registerRoomObserver(@RequestBody roomObservorReq req) {
		if(req.getCallbackurl()==null || req.getRoomprefix()==null)
			return "parameter error";
		return rtcmsService.registerRoomObserver(req);
	}

	//注册成为28181消息接收者
	@RequestMapping(value="/rtc/registerObserver",method=RequestMethod.POST)
	public String registerObserver() {	return rtcmsService.registerObserver(); }

	//发送SIP MESSAGE消息
	@RequestMapping(value="/rtc/sendSIPMessage",method=RequestMethod.POST)
	public String sendSIPMessage(@RequestBody sendSipReq req) {	return rtcmsService.sendSIPMessage(req); }

	//发送SIP SUBSCRIBE消息
	@RequestMapping(value="/rtc/sendSIPSubscribe",method=RequestMethod.POST)
	public String sendSIPSubscribe(@RequestBody sendSipReq req) {	return rtcmsService.sendSIPSubscribe(req); }

	//发送SIP SUBSCRIBE消息
	@RequestMapping(value="/rtc/securityURL",method=RequestMethod.POST)
	public String securityURL(@RequestBody securityUrlReq req) {	return rtcmsService.securityURL(req); }

	//QOS查询
	@RequestMapping(value="/rtc/queryQOS",method=RequestMethod.POST)
	public String queryQOS(@RequestBody queryQosReq req) {	return rtcmsService.queryQOS(req); }

	//zwmp资源使用量查询
	@RequestMapping(value="/rtc/zwmpUsage",method=RequestMethod.POST)
	public String zwmpUsage(@RequestBody zwmpUsageReq req) {	return rtcmsService.zwmpUsage(req); }

	//zwmp使用端口查询
	@RequestMapping(value="/rtc/zwmpPort",method=RequestMethod.POST)
	public String zwmpPort(@RequestBody zwmpUsageReq req) {	return rtcmsService.zwmpPort(req); }

	//zwmp使用会议和通道概要查询
	@RequestMapping(value="/rtc/zwmpChans",method=RequestMethod.POST)
	public String zwmpChans(@RequestBody zwmpUsageReq req) {	return rtcmsService.zwmpChans(req); }

	//zwmp通道详情
	@RequestMapping(value="/rtc/zwmpChanDetail",method=RequestMethod.POST)
	public String zwmpChanDetail(@RequestBody zwmpChanDetailReq req) {	return rtcmsService.zwmpChanDetail(req); }

	@RequestMapping(value="/rtc/test",method=RequestMethod.POST)
	public void zwmpChanDetail(@RequestBody String req) {	log.info("test:\n" + req); }

	//登录，默认都通过
	@RequestMapping(value="/user/login",method=RequestMethod.POST)
	public Map<String, Object> userLogin(@RequestBody LoginReq req) {
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> data = new HashMap<>();
		if(req.getUserName().equalsIgnoreCase("zwan") && req.getPassword().equals("Zen@dvlp")){
			data.put("token", req.getUserName()+"_token");
			result.put("data", data);
			result.put("code", 200);
		}else{
			result.put("code", 403);
		}
		
		return result;
	}


	//oam-web登录鉴权
	@RequestMapping(value="/user/info",method=RequestMethod.GET)
	public Map<String, Object> info() {
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> data = new HashMap<>();
		List<String> roles = new ArrayList<String>();
		roles.add("admin");
		data.put("roles", roles);
		data.put("introduction", "rtcms administrator");
		data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
		data.put("name", "Admin");
		result.put("data", data);
		result.put("code", 200);
		return result;
	}

	//oam-web登出
	@RequestMapping(value="/user/logout",method=RequestMethod.POST)
	public Map<String, Object> logout() {
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> data = new HashMap<>();
		result.put("data", data);
		result.put("code", 200);
		return result;
	}

}
